<template>
  <div class="article-meta">
    <nuxt-link :to="{
      name: 'profile',
      params: {
        username: article.author.username
      }
    }">
      <img :src="article.author.image" />
    </nuxt-link>
    <div class="info">
      <nuxt-link class="author" :to="{
        name: 'profile',
        params: {
          username: article.author.username
        }
      }">
        {{ article.author.username }}
      </nuxt-link>
      <span class="date">{{ article.createdAt | date('MMM DD, YYYY') }}</span>
    </div>
    <button
      v-if="user && user.username !== article.author.username"
      class="btn btn-sm btn-outline-secondary"
      :class="{
        active: article.author.following
      }"
      :disabled="followDisabled"
      @click="onFollowUser"
    >
      <i class="ion-plus-round"></i>
      &nbsp;
      {{(article.author.following ? 'Unfollow ' : 'Follow ') + article.author.username}}
    </button>
    &nbsp;&nbsp;
    <button
      v-if="user && user.username !== article.author.username"
      class="btn btn-sm"
      :class="{
        active: article.favorited,
        ...toggleFavoriteButtonClasses
      }"
      :disabled="favoriteDisabled"
      @click="onFavorite"
    >
      <i class="ion-heart"></i>
      &nbsp;
      {{favoriteState ? 'Unfavorite' : 'Favorite'}} Post <span class="counter">({{article.favoritesCount}})</span>
    </button>
    <button
      v-if="user && user.username === article.author.username"
      class="btn btn-outline-secondary btn-sm"
      @click="onEdit"
    >
      <i class="ion-edit"></i> Edit Article
    </button>
    <button
      v-if="user && user.username === article.author.username"
      class="btn btn-outline-danger btn-sm"
      @click="onDelete"
    >
      <i class="ion-trash-a"></i> Delete Article
    </button>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import {
  addFavorite,
  deleteFavorite,
  deleteArticles
} from "@/api/article";
import {
  deleteFollow,
  addFollow
} from "@/api/profile";

export default {
  name: 'ArticleMeta',
  data() {
    return {
      followDisabled: false,
      favoriteDisabled: false
    }
  },
  props: {
    article: {
      type: Object,
      required: true
    },
    favoriteArticles: {
      type: Array,
      default: ()=>[]
    }
  },
  computed: {
    ...mapState(['user']),
    favoriteState(){
      return this.favoriteArticles.includes(this.article.slug)
    },
    toggleFavoriteButtonClasses() {
      return {
        "btn-primary": this.favoriteArticles.includes(this.article.slug),
        "btn-outline-primary": !this.favoriteArticles.includes(this.article.slug)
      };
    },
  },
  methods: {
    async onFollowUser() {
      this.followDisabled = true;
      if (this.article.author.following) {
        await deleteFollow(this.article.author.username);
        this.$emit('follow', false)
      } else {
        await addFollow(this.article.author.username);
        this.$emit('follow', true)
      }
      this.followDisabled = false;
      
    },
    async onFavorite() {
      this.favoriteDisabled = true;
      if (this.favoriteState) {
        // 取消点赞
        await deleteFavorite(this.article.slug);
        this.$emit('favor', false)
      } else {
        // 添加点赞
        await addFavorite(this.article.slug);
        this.$emit('favor', true)
      }
      this.favoriteDisabled = false;
    },
    onEdit() {
      this.$router.push(`/editor/${this.article.slug}`)
    },
    async onDelete() {
      await deleteArticles(this.article.slug)
      this.$router.push('/')
    }
  },
}
</script>

<style>

</style>
